import { AxiosProgressEvent } from 'axios';

import { PaginationProps, PaginationResponse, QA } from '@/common';
import {
  FeedbackQuestionType,
  FreqQuestionType,
  SpecialtyType,
  WordType,
  UnknowQuestionType,
  FaqDataset,
  GovStatisType,
  TodoGov,
  KnowSimilarityCollection,
  ProcessSimilarityReq,
  MarkQuestionReq,
  KnowCollection,
  MarkQuestionResItem,
  AnnotationType,
  AnnotationOpType,
  // connectorTypeRes,
  connectorTypeReq,
  validateReq,
  FilePreInfoType,
  UpdateExpireReq,
  KnowUnknownReq,
  AnnotationTypeItem,
  DislikeQuestionAnnotationType,
  IgnoreAutoCorrectType,
} from '@/types/know/know';

import apiClient from '../apiClient';

export enum KnowApi {
  // 问答
  GetUnknownQuestions = '/openapi/v1/unknown/list',
  GetFrequentQuestions = '/openapi/v1/frequent/list',
  GetFeedbackQuestions = '/openapi/v1/feedback/list',

  // 问题详情
  GetUnknownQuestionDetail = '/openapi/v1/unknown/detail',
  GetFrequentQuestionDetail = '/openapi/v1/frequent/detail',
  GetFeedbackQuestionDetail = '/openapi/v1/feedback/detail',

  // 标注
  MarkQuestion = '/openapi/v1/dataset/data/annotation',
  GetAnnotations = '/openapi/v1/dataset/data/annotation/list',
  PublishAnnotation = '/openapi/v1/dataset/data/annotation/publish',
  WithdrawAnnotation = '/openapi/v1/dataset/data/annotation/withdraw',

  // 词条
  Specialty = '/openapi/v1/terms/specialization',
  SensitiveWords = '/openapi/v1/terms/censored',
  Synonym = '/openapi/v1/terms/synonym',

  // 词条模板下载
  DownloadEntryTemplate = '/openapi/v1/terms/template/download',

  // 词条批量导入
  ImportSpecialty = '/openapi/v1/terms/specialization/import',
  ImportSensitiveWords = '/openapi/v1/terms/censored/import',
  ImportSynonym = '/openapi/v1/terms/synonym/import',

  GetFaqDataset = '/openapi/v1/dataset/data/faq',

  GetGovStatis = '/openapi/v1/dashboard/data/total',
  GetTodoGovList = '/openapi/v1/dashboard/data/list',

  // 文档
  // GetSimilarityCollection = '/openapi/v1/similarity/list',
  GetSimilarityCollection = '/openapi/v2/knowledges/similarity/list',
  GetSimilarityCollectionPreviewInfos = '/openapi/v1/similarity/preview/infos',
  ProcessCimilarity = '/openapi/v1/similarity/process',
  ExpiredCollection = '/openapi/v1/dataset/collection/expired',

  // 数据连接器
  GetConnectorList = '/openapi/v2/connector',
  GetConnector = '/api_know/openapi/v2/connector',
  ValidateConnector = '/api_know/openapi/v2/connector/validate',
  TaskConnector = '/api_know/openapi/v2/connector/task',

  // 知识治理
  GetUnknowCount = '/openapi/v2/knowledge/countpage/unknow/count',
  GetfeedbackCount = '/openapi/v2/knowledge/countpage/feedback/count',
  GetExpiredCount = '/openapi/v2/knowledge/countpage/expired/count',
  GetFrequency = '/openapi/v2/knowledge/countpage/frequency/account',
  GetFrequencyList = '/openapi/v2/knowledge/countpage/frequency/count/list',

  // AI未知问
  GetUnknownList = '/openapi/v2/knowledges/unknow/list',
  UnknoOverlook = '/openapi/v2/knowledges/unknow/ignore',
  UpdateUnknowQuestion = '/openapi/v2/knowledges/unknow/title',
  AICreat = '/openapi/v2/knowledges/unknow/aicreat',
  AIback = '/openapi/v2/knowledges/aiembellish/aiback',
  GetFaqDatasetList = '/openapi/v2/knowledges/data/faq',
  UpdateUnknowQuestionAnnotation = '/openapi/v2/knowledges/unknow/data/annotation',
  DeleteUnknowQuestion = '/openapi/v2/knowledges/unknow/delete',
  // 用户点踩问
  GetUserDislikeList = '/openapi/v2/knowledges/feedback/list',
  DislikeAICreat = '/openapi/v2/knowledges/feedback/aicreat',
  UpdateDislikeQuestionAnnotation = '/openapi/v2/knowledges/feedback/data/annotation',
  // 老化列表
  GetExpiredList = '/openapi/v2/knowledges/experiod/list',
  // 文档优化
  Optimizedist = '/openapi/v2/knowledges/optimize/detail',
  AutoCorrectList = '/openapi/v2/knowledges/optimize/autoCorrect/list',
  SensitiveDetectionList = '/openapi/v2/knowledges/optimize/sensitiveDetection/list',
  IgnoreAutoCorrect = '/openapi/v2/knowledges/optimize/ignore',
  UpdateOptimize = '/openapi/v2/knowledges/optimize/update',
  GetSimilarityList = '/openapi/v2/knowledges/optimize/similarity/list',
  SimilarityIngore = '/openapi/v2/knowledges/optimize/similarity/ingore',
}

// 知识看板
const getunknowCount = (params: KnowUnknownReq) =>
  apiClient.get({
    url: KnowApi.GetUnknowCount,
    params,
  });
const getfeedbackCount = (params: KnowUnknownReq) =>
  apiClient.get({
    url: KnowApi.GetfeedbackCount,
    params,
  });
const getExpiredCount = (params: KnowUnknownReq) =>
  apiClient.get({
    url: KnowApi.GetExpiredCount,
    params,
  });
const getFrequency = (params: KnowUnknownReq) =>
  apiClient.get({
    url: KnowApi.GetFrequency,
    params,
  });
const getFrequencyList = (params: KnowUnknownReq) =>
  apiClient.get({
    url: KnowApi.GetFrequencyList,
    params,
  });
// 获取未知问接口
const getUnknownList = (params: PaginationProps) =>
  apiClient.get({
    url: KnowApi.GetUnknownList,
    params,
  });
// 处理未知问忽略
const unknoOverlook = (data: { keyQuestionQaId: string[]; appId: string }) =>
  apiClient.post({
    url: KnowApi.UnknoOverlook,
    data,
  });
// 修改名称
const updateUnknowQuestion = (data: { keyQuestionQaId: string; appId: string; title: string }) =>
  apiClient.post({
    url: KnowApi.UpdateUnknowQuestion,
    data,
  });
// AI润色
const aICreat = (data: { question: string; appId: string; keyQuestionQaId: string }) =>
  apiClient.post({
    url: KnowApi.AICreat,
    data,
  });
// 撤回润色
const AIback = (data: { keyQuestionQaId: string; appId: string; question: string }) =>
  apiClient.post({
    url: KnowApi.AIback,
    data,
  });
// 获取faq列表
const getFaqDatasetList = (params: PaginationProps) =>
  apiClient.get({
    url: KnowApi.GetFaqDatasetList,
    params,
  });
// 更新未知问标注
const updateUnknowQuestionAnnotation = (data: AnnotationTypeItem) =>
  apiClient.post({
    url: KnowApi.UpdateUnknowQuestionAnnotation,
    data,
  });
// 删除未知问
const deleteUnknowQuestion = (data: { qaIdList: string[]; appId: string }) =>
  apiClient.post({
    url: KnowApi.DeleteUnknowQuestion,
    data,
  });
// 点踩列表
const getUserDislikeList = (params: PaginationProps) =>
  apiClient.get({
    url: KnowApi.GetUserDislikeList,
    params,
  });
// 点踩AI润色
const dislikeAICreat = (data: {
  question: string;
  appId: string;
  keyQuestionQaId: string;
  answer: string;
}) =>
  apiClient.post({
    url: KnowApi.DislikeAICreat,
    data,
  });
// 更新点踩标注
const updateDislikeQuestionAnnotation = (data: DislikeQuestionAnnotationType) =>
  apiClient.post({
    url: KnowApi.UpdateDislikeQuestionAnnotation,
    data,
  });
// 老化列表
const getExpiredList = (params: PaginationProps) =>
  apiClient.get({
    url: KnowApi.GetExpiredList,
    params,
  });
// 文档优化
const getOptimizedist = (params: PaginationProps) =>
  apiClient.get({
    url: KnowApi.Optimizedist,
    params,
  });
// 错别字列表
const autoCorrectList = (collectionId: string, appId: string) =>
  apiClient.get({
    url: KnowApi.AutoCorrectList,
    params: { collectionId, appId },
  });
// 忽略
const ignoreAutoCorrect = (data: IgnoreAutoCorrectType) =>
  apiClient.post({
    url: KnowApi.IgnoreAutoCorrect,
    data,
  });
// 更新
const updateOptimize = (data: IgnoreAutoCorrectType) =>
  apiClient.post({
    url: KnowApi.UpdateOptimize,
    data,
  });
// 敏感词列表
const sensitiveDetectionList = (collectionId: string, appId: string) =>
  apiClient.get({
    url: KnowApi.SensitiveDetectionList,
    params: { collectionId, appId },
  });
// 重复文档详情
const getSimilarityList = (
  collectionId: string,
  appId: string,
  page?: number,
  pageSize?: number,
  offset?: number,
) =>
  apiClient.get({
    url: KnowApi.GetSimilarityList,
    params: { collectionId, appId, page, pageSize, offset },
  });
// 忽略重复文档
const similarityIngore = (data: { datasetId: string[]; collectionId: string[] }) =>
  apiClient.post({
    url: KnowApi.SimilarityIngore,
    data,
  });

/**
 * 同步连接器
 * @param params
 * @returns
 */
const taskConnector = (data: validateReq) => apiClient.post({ url: KnowApi.TaskConnector, data });

/**
 * 校验连接
 * @param params
 * @returns
 */
const validateConnector = (data: validateReq) =>
  apiClient.post({ url: KnowApi.ValidateConnector, data });

/**
 * 删除连接器
 * @param params
 * @returns
 */
const delConnectorList = (id: string) => apiClient.delete({ url: `${KnowApi.GetConnector}/${id}` });

/**
 * 更新连接器
 * @param params
 * @returns
 */
const updataConnectorList = (id: string, data: string[]) =>
  apiClient.put({ url: `${KnowApi.GetConnector}/${id}`, data });

/**
 * 创建连接器
 * @param params
 * @returns
 */
const addConnectorList = (data: string[]) => apiClient.post({ url: KnowApi.GetConnector, data });

/**
 * 获取数据连接器列表
 * @param params
 * @returns
 */
const getConnectorList = (params: PaginationProps) =>
  apiClient.get<PaginationResponse<connectorTypeReq>>({
    url: KnowApi.GetConnectorList,
    params,
  });

/**
 * 获取未知问结果
 * @param params
 * @returns
 */
const getUnknownQuestions = (params: PaginationProps) =>
  apiClient.get<PaginationResponse<UnknowQuestionType>>({
    url: KnowApi.GetUnknownQuestions,
    params,
  });

/**
 * 获取未知问详情
 * @param id
 * @returns
 */
const getUnknownQuestionDetail = (id: string) =>
  apiClient.get<UnknowQuestionType>({ url: `${KnowApi.GetUnknownQuestionDetail}/${id}` });

/**
 * 获取高频问结果
 * @param params
 * @returns
 */
const getFrequentQuestions = (params: PaginationProps) =>
  apiClient.get<PaginationResponse<FreqQuestionType>>({
    url: KnowApi.GetFrequentQuestions,
    params,
  });

/**
 * 获取高频问详情
 * @param id
 * @returns
 */
const getFrequentQuestionDetail = (id: string) =>
  apiClient.get<FreqQuestionType>({ url: `${KnowApi.GetFrequentQuestionDetail}/${id}` });

/**
 * 获取点赞点踩问结果
 * @param params
 * @returns
 */
const getFeedbackQuestions = (params: PaginationProps) =>
  apiClient.get<PaginationResponse<FeedbackQuestionType>>({
    url: KnowApi.GetFeedbackQuestions,
    params,
  });

/**
 * 获取点踩问详情
 * @param id
 * @returns
 */
const getFeedbackQuestionDetail = (id: string) =>
  apiClient.get<FreqQuestionType>({ url: `${KnowApi.GetFeedbackQuestionDetail}/${id}` });

/**
 * 获取专业词
 * @param params
 * @returns
 */
const getSpecialty = (params: PaginationProps) =>
  apiClient.get<PaginationResponse<SpecialtyType>>({ url: KnowApi.Specialty, params });

/**
 * 导入专业词
 * @param data
 * @param onUploadProgress
 * @returns
 */
const importSpecialtyFile = (
  data: FormData,
  onUploadProgress: (progressEvent: AxiosProgressEvent) => void,
) =>
  apiClient.post({
    url: KnowApi.ImportSpecialty,
    withCredentials: true,
    timeout: 600000,
    data,
    onUploadProgress,
    headers: { 'Content-Type': 'multipart/form-data' },
  });

/**
 * 添加专业词
 * @param params
 * @returns
 */
const addSpecialty = (data: string[]) =>
  apiClient.post({ url: KnowApi.Specialty, data: { datas: data } });

/**
 * 更新专业词
 * @param data
 * @returns
 */
const updateSpecialty = (id: string, text: string) =>
  apiClient.put({ url: `${KnowApi.Specialty}/${id}`, data: { text } });

/**
 * 删除专业词
 * @param id
 * @returns
 */
const delSpecialty = (id: string) => apiClient.delete({ url: `${KnowApi.Specialty}/${id}` });

/**
 * 获取敏感词
 * @param params
 * @returns
 */
const getSensitiveWords = (params: PaginationProps) =>
  apiClient.get<PaginationResponse<WordType>>({ url: KnowApi.SensitiveWords, params });

/**
 * 新增敏感词组
 * @param params
 * @returns
 */
const addSensitiveWords = (data: WordType) => apiClient.post({ url: KnowApi.SensitiveWords, data });

/**
 * 更新敏感词组
 * @param params
 * @returns
 */
const updateSensitiveWords = (data: WordType) =>
  apiClient.put({ url: `${KnowApi.SensitiveWords}/${data._id}`, data });

/**
 * 删除敏感词
 * @param id
 * @returns
 */
const delSensitiveWords = (id: string) =>
  apiClient.delete({ url: `${KnowApi.SensitiveWords}/${id}` });

/**
 * 导入敏感词
 * @param data
 * @param onUploadProgress
 * @returns
 */
const importSensitiveFile = (
  data: FormData,
  onUploadProgress: (progressEvent: AxiosProgressEvent) => void,
) =>
  apiClient.post({
    url: KnowApi.ImportSensitiveWords,
    withCredentials: true,
    timeout: 600000,
    data,
    onUploadProgress,
    headers: { 'Content-Type': 'multipart/form-data' },
  });

/**
 * 获取同义词
 * @param params
 * @returns
 */
const getSynonym = (params: PaginationProps) =>
  apiClient.get<PaginationResponse<WordType>>({ url: KnowApi.Synonym, params });

/**
 * 新增同义词
 * @param data
 * @returns
 */
const addSynonym = (data: WordType) => apiClient.post({ url: KnowApi.Synonym, data });

/**
 * 更新同义词
 * @param data
 * @returns
 */
const updateSynonym = (data: WordType) =>
  apiClient.put({ url: `${KnowApi.Synonym}/${data._id}`, data });

/**
 * 删除同义词
 * @param params
 * @returns
 */
const delSynonym = (id: string) => apiClient.delete({ url: `${KnowApi.Synonym}/${id}` });

/**
 * 导入同义词
 * @param data
 * @param onUploadProgress
 * @returns
 */
const importSynonymFile = (
  data: FormData,
  onUploadProgress: (progressEvent: AxiosProgressEvent) => void,
) =>
  apiClient.post({
    url: KnowApi.ImportSynonym,
    withCredentials: true,
    timeout: 600000,
    data,
    onUploadProgress,
    headers: { 'Content-Type': 'multipart/form-data' },
  });

/**
 * 下载词条模板
 * @param type 模板类型
 * @returns
 */
const downloadEntryTemplate = (type: string) =>
  apiClient.get({ url: KnowApi.DownloadEntryTemplate, params: { type } });

/**
 * 获取Faq知识库
 * @param params 分页请求
 * @returns
 */
const getFaqDataset = (params: PaginationProps) =>
  apiClient.get<PaginationResponse<FaqDataset>>({ url: KnowApi.GetFaqDataset, params });

/**
 * 获取待治理统计
 * @returns
 */
const getGovStatisics = (params: PaginationProps) =>
  apiClient.get<GovStatisType>({ url: KnowApi.GetGovStatis, params });

/**
 * 获取待治理统计对象列表
 * @returns
 */
const getTodoGovList = (params: PaginationProps) =>
  apiClient.get<PaginationResponse<TodoGov>>({ url: KnowApi.GetTodoGovList, params });

/**
 * 获取重复文档
 * @param params
 * @returns
 */
const getSimilarity = (params: PaginationProps) =>
  apiClient.get<PaginationResponse<KnowSimilarityCollection>>({
    url: KnowApi.GetSimilarityCollection,
    params,
  });

/**
 * 获取重复文档详情
 * @param params
 * @returns
 */
const getSimilarityPreviewInfo = (params: { id: string, appId: string }) =>
  apiClient.get<Record<string, FilePreInfoType>>({
    url: `${KnowApi.GetSimilarityCollectionPreviewInfos}`,
    params,
  });

/**
 * 处理重复知识接口（保留/去除）
 * @param data
 * @returns
 */
const postProcessSimilarity = (data: ProcessSimilarityReq) =>
  apiClient.post({ url: KnowApi.ProcessCimilarity, data });

/**
 * 获取老化文档
 * @param params
 * @returns
 */
const getExpired = (params: PaginationProps) =>
  apiClient.get<PaginationResponse<KnowCollection>>({
    url: KnowApi.ExpiredCollection,
    params,
  });

/**
 * 设置老化文档过期时间
 * @param params
 * @returns
 */
const postUpdateExpired = (data: UpdateExpireReq) =>
  apiClient.post<PaginationResponse<KnowCollection>>({
    url: KnowApi.ExpiredCollection,
    data,
  });

/**
 * 问答标注/忽略
 * @param data
 * @returns
 */
const postMarkQuestion = (data: MarkQuestionReq) =>
  apiClient.post<MarkQuestionResItem[]>({ url: KnowApi.MarkQuestion, data });

/**
 * 更新标注结果
 * @param data
 * @returns
 */
const updateMarkQuestion = (id: string, data: QA) =>
  apiClient.put({ url: `${KnowApi.MarkQuestion}/${id}`, data });

/**
 * 获取问答标注结果
 * @param data
 * @returns
 */
const getAnnotations = (params: PaginationProps) =>
  apiClient.get<PaginationResponse<AnnotationType>>({ url: KnowApi.GetAnnotations, params });

/**
 * 发布
 * @param data
 * @returns
 */
const publishAnnotation = (data: AnnotationOpType) =>
  apiClient.post({ url: KnowApi.PublishAnnotation, data });

/**
 * 发布
 * @param data
 * @returns
 */
const withdrawAnnotation = (data: AnnotationOpType) =>
  apiClient.post({ url: KnowApi.WithdrawAnnotation, data });

export default {
  getunknowCount,
  getfeedbackCount,
  getExpiredCount,
  getFrequency,
  getFrequencyList,
  getUnknownList,
  unknoOverlook,
  updateUnknowQuestion,
  aICreat,
  AIback,
  getFaqDatasetList,
  updateUnknowQuestionAnnotation,
  getUserDislikeList,
  dislikeAICreat,
  deleteUnknowQuestion,
  updateDislikeQuestionAnnotation,
  getExpiredList,
  getOptimizedist,
  autoCorrectList,
  ignoreAutoCorrect,
  updateOptimize,
  sensitiveDetectionList,
  getSimilarityList,
  similarityIngore,

  getUnknownQuestions,
  getUnknownQuestionDetail,
  getFrequentQuestions,
  getFrequentQuestionDetail,
  getFeedbackQuestions,
  getFeedbackQuestionDetail,
  getSpecialty,
  getSensitiveWords,
  getSynonym,
  addSpecialty,
  delSpecialty,
  updateSpecialty,
  addSynonym,
  updateSynonym,
  delSynonym,
  addSensitiveWords,
  updateSensitiveWords,
  delSensitiveWords,
  importSpecialtyFile,
  importSensitiveFile,
  importSynonymFile,
  getFaqDataset,
  getGovStatisics,
  getTodoGovList,
  getSimilarity,
  getSimilarityPreviewInfo,
  getExpired,
  postUpdateExpired,
  postProcessSimilarity,
  postMarkQuestion,
  updateMarkQuestion,
  downloadEntryTemplate,
  getAnnotations,
  publishAnnotation,
  withdrawAnnotation,
  getConnectorList,
  addConnectorList,
  updataConnectorList,
  delConnectorList,
  validateConnector,
  taskConnector,
};
